package com.fowo.api.flow.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.flow.model.TemplateGroupVo;
import com.fowo.api.flow.service.FromGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @fileName: FromGroupController
 * @Description: 代码目的，作用，如何工作
 * @Author: pzh
 * @Date: 2023/4/6 0006 11:08
 */
@Tag(name = "表单分组")
@RequestMapping("/api/form")
@RestController
public class FromGroupController {

    @Resource
    private FromGroupService fromGroupService;

    /**
     * 查询表单组
     *
     * @return 表单组数据
     */
    @Operation(summary = "查询表单组")
    @GetMapping("/group")
    public R<List<TemplateGroupVo>> getFormGroups() {
        return R.data(fromGroupService.getFormGroups());
    }


    /**
     * 表单及分组排序
     *
     * @param groups 分组数据
     * @return 排序结果
     */
    @Operation(summary = "表单及分组排序")
    @PostMapping("/sort")
    public R formGroupsSort(@RequestBody @Validated List<TemplateGroupVo> groups) {
        return R.run(() -> fromGroupService.formGroupsSort(groups));
    }


    /**
     * 修改分组
     *
     * @param id   分组ID
     * @param name 分组名
     * @return 修改结果
     */
    @Operation(summary = "修改分组")
    @PutMapping("update")
    public R updateFormGroupName(@RequestParam(value = "id", required = false) Integer id,
                                 @RequestParam(value = "name", required = false) String name) {
        return R.run(() -> fromGroupService.updateFormGroupName(id, name));
    }

    /**
     * 新增表单分组
     *
     * @param name 分组名
     * @return 添加结果
     */
    @Operation(summary = "新增表单分组")
    @PostMapping("/save")
    public R createFormGroup(@RequestParam(value = "name", required = false) String name) {
        return R.run(() -> fromGroupService.createFormGroup(name));
    }

    /**
     * 删除分组
     *
     * @param id 分组ID
     * @return 删除结果
     */
    @Operation(summary = "删除分组")
    @DeleteMapping("{id}")
    public R deleteFormGroup(@PathVariable Integer id) throws Exception {
        return R.run(() -> fromGroupService.deleteFormGroup(id));
    }

    /**
     * 编辑表单
     *
     * @param templateId 摸板ID
     * @param type       类型 stop using delete
     * @return 操作结果
     */
    @Operation(summary = "编辑表单")
    @PutMapping("/edit")
    public R updateForm(
            @RequestParam(value = "templateId", required = false) String templateId,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "groupId", required = false) Integer groupId) {
        return R.run(() -> fromGroupService.updateForm(templateId, type, groupId));
    }
}
